package com.teyao.teyao_back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.teyao.teyao_back.entity.Orders;
import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@RestController
@CrossOrigin
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/createNewOrder")
    public ResponseMessage createNewOrder(Orders orders) {
        ResponseMessage responseMessage = new ResponseMessage();
        if (!orders.getClient().isBlank() && !orders.getConsignee().isBlank() && !orders.getConsigneePhone().isBlank() && !orders.getAddress().isBlank()) {
            String orderId = UUID.randomUUID().toString().substring(0, 8) + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            orders.setOrderId(orderId);
            responseMessage.setSuccess(orderService.save(orders));
            responseMessage.setData(orders);

        } else {
            responseMessage.setMsg("信息填写不完全");
        }
        return responseMessage;
    }

    @PostMapping("/deleteOrder")
    public ResponseMessage deleteOrder(String orderId) {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        responseMessage.setSuccess(orderService.remove(queryWrapper));
        responseMessage.setMsg("未选择商品");
        return responseMessage;
    }

    @PostMapping("/updateEndTime")
    public ResponseMessage updateEndTime(String orderId, String deliverTime) {
        ResponseMessage responseMessage = new ResponseMessage();
        Orders orders = new Orders();
        orders.setDeliverTime(deliverTime);
        UpdateWrapper<Orders> ordersUpdateWrapper = new UpdateWrapper<>();
        ordersUpdateWrapper.eq("order_id", orderId);
        responseMessage.setSuccess(orderService.update(orders, ordersUpdateWrapper));
        return responseMessage;
    }

    //    查所有
    @GetMapping("/list")
    public List<Orders> selectAll() {
        return orderService.list();
    }

    //更新
    @PostMapping("/updateinfo")
    public ResponseMessage getUpdateInfo(Orders orders) {
        return orderService.updateInfo(orders);
    }


}
